package com.example.vv.model.accountItem.base.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.vv.R;
import com.example.vv.fragmentWork.entity.WxarticleEntity;
import com.example.vv.model.accountDetails.AIDActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class AIAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<WxarticleEntity.DatasBean> wxarticleEntities = new ArrayList<>();
    Context context;
    boolean flag = false;

    public AIAdapter(Context context) {

        this.context = context;
    }

    class AIHolder extends RecyclerView.ViewHolder {
        TextView home_type;
        TextView home_time;
        TextView home_content;
        TextView home_bottom_type;
        TextView home_bottom_type1;
        RelativeLayout rl;
        TextView home_type_bd;

        public AIHolder(@NonNull View itemView) {
            super(itemView);
            home_type = itemView.findViewById(R.id.home_type);
            home_time = itemView.findViewById(R.id.home_time);
            home_content = itemView.findViewById(R.id.home_content);
            home_bottom_type = itemView.findViewById(R.id.home_bottom_type);
            home_bottom_type1 = itemView.findViewById(R.id.home_bottom_type1);
            home_type_bd = itemView.findViewById(R.id.home_type_bd);
            rl = itemView.findViewById(R.id.rl);
        }
    }

    public void clear() {
        wxarticleEntities.clear();
    }


    public void addData(List<WxarticleEntity.DatasBean> list) {
        wxarticleEntities.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_account, parent, false);
        AIHolder holder = new AIHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        WxarticleEntity.DatasBean wxarticleEntity = wxarticleEntities.get(position);
        ((AIHolder) holder).home_type.setText(wxarticleEntity.getAuthor());
        ((AIHolder) holder).home_time.setText(wxarticleEntity.getNiceDate());
        if (wxarticleEntity.getNiceDate().equals("1天前")||wxarticleEntity.getNiceDate().equals("刚刚")) {
            ((AIHolder) holder).home_type_bd.setText("新");
        }else {
            ((AIHolder) holder).home_type_bd.setVisibility(View.GONE);
        }
        ((AIHolder) holder).home_content.setText(wxarticleEntity.getTitle());
        ((AIHolder) holder).home_bottom_type.setText(wxarticleEntity.getSuperChapterName());
        ((AIHolder) holder).home_bottom_type1.setText(wxarticleEntity.getChapterName());
        ((AIHolder) holder).rl.setOnClickListener(v->{
            EventBus.getDefault().postSticky(wxarticleEntity);
            context.startActivity(new Intent(context,AIDActivity.class));
        });
    }

    @Override
    public int getItemCount() {
        return wxarticleEntities.size() == 0 ? 0 : wxarticleEntities.size();
    }
}
